package cn.edu.stu.syllabus.adapters;

import android.support.v4.content.ContextCompat;
import android.text.Html;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseViewHolder;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.edu.stu.syllabus.R;
import cn.edu.stu.syllabus.application.SyllabusApplication;
import cn.edu.stu.syllabus.base.BaseQuickClickAdapter;
import cn.edu.stu.syllabus.repository.takeout.local.SubMenuEntry;

/**
 * Created by daidaijie on 2017/4/4.
 */

public class SubMenuAdapter extends BaseQuickClickAdapter<SubMenuEntry, SubMenuAdapter.ViewHolder> {

    private int mSelectItemPosition;

    @Override
    protected ViewHolder createBaseViewHolder(View view) {
        return new ViewHolder(view);
    }

    public SubMenuAdapter(List<SubMenuEntry> data) {
        super(R.layout.item_sub_menu, data);
        mSelectItemPosition = 0;
    }

    @Override
    protected void convert(final ViewHolder helper, final SubMenuEntry item) {
        if (mSelectItemPosition == helper.getAdapterPosition()) {
            helper.mSubMenuTitleTextView.setText(Html.fromHtml("<b>" + item.getName() + "</b>"));
            helper.mSubMenuTitleTextView.setBackgroundColor(ContextCompat.getColor(
                    SyllabusApplication.getContext(), R.color.colorBackground
            ));
        } else {
            helper.mSubMenuTitleTextView.setText(item.getName());
            helper.mSubMenuTitleTextView.setBackgroundColor(ContextCompat.getColor(
                    SyllabusApplication.getContext(), R.color.colorBackgroundDark
            ));
        }

        helper.mSubMenuTitleTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnItemClickListener != null) {
                    mOnItemClickListener.onItemClick(item, helper.getAdapterPosition());
                }
            }
        });

    }

    public int getSelectItemPosition() {
        return mSelectItemPosition;
    }

    public void setSelectItemPosition(int selectItemPosition) {
        mSelectItemPosition = selectItemPosition;
    }

    public class ViewHolder extends BaseViewHolder {

        @BindView(R.id.subMenuTitleTextView)
        TextView mSubMenuTitleTextView;

        public ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}
